<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test generating, importing and exporting RSA keys for RSA-OAEP. Test that they can't be used with another algorithm.");

jsTestIsAsync = true;

var algorithmKeyGen = {
    name: "RSA-OAEP",
    hash: {name: "sha-1"},
    // RsaKeyGenParams
    modulusLength: 512,
    publicExponent: new Uint8Array([0x01, 0x00, 0x01]),  // Equivalent to 65537
};
var extractable = true;

debug("Generating a key pair...");
crypto.subtle.generateKey(algorithmKeyGen, extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]).then(function(result) {
    keyPair = result;
    shouldBe("keyPair.toString()", "'[object Object]'");
    shouldBe("keyPair.publicKey.type", "'public'");
    shouldBe("keyPair.publicKey.algorithm.name", "'RSA-OAEP'");
    shouldBe("keyPair.publicKey.algorithm.modulusLength", "512");
    shouldBe("keyPair.publicKey.usages", '["encrypt", "wrapKey"]');
    shouldBe("bytesToHexString(keyPair.publicKey.algorithm.publicExponent)", "'010001'");
    shouldBe("keyPair.publicKey.algorithm.hash.name", "'SHA-1'");
    shouldBe("keyPair.privateKey.type", "'private'");
    shouldBe("keyPair.privateKey.algorithm.name", "'RSA-OAEP'");
    shouldBe("keyPair.privateKey.algorithm.modulusLength", "512");
    shouldBe("keyPair.privateKey.usages", '["decrypt", "unwrapKey"]');
    shouldBe("bytesToHexString(keyPair.privateKey.algorithm.publicExponent)", "'010001'");
    shouldBe("keyPair.privateKey.algorithm.hash.name", "'SHA-1'");

    debug("\nUsing the RSA-OAEP private key for AES-CBC encryption...");
    iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");

    return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, keyPair.privateKey, hexStringToUint8Array("00"));
}, failAndFinishJSTest).then(failAndFinishJSTest, function(result) {
    logError(result);

    debug("\nUsing the RSA-OAEP public key for AES-CBC encryption...");
    return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, keyPair.publicKey, hexStringToUint8Array("00"));
}, failAndFinishJSTest).then(failAndFinishJSTest, function(result) {
    logError(result);
    debug("\nTrying to export keys to raw...");
    return crypto.subtle.exportKey('raw', keyPair.publicKey);
}, failAndFinishJSTest).then(failAndFinishJSTest, function(result) {
    logError(result);
    testPassed("Promise rejected for exporting public key");
    return crypto.subtle.exportKey('raw', keyPair.privateKey);
}).then(failAndFinishJSTest, function(result) {
    logError(result);
    testPassed("Promise rejected for exporting private key");

    debug("\nExporting public key to JWK...");
    return crypto.subtle.exportKey("jwk", keyPair.publicKey);
}).then(function(result) {
    jwkPublicKey = result;
    shouldBe("jwkPublicKey.alg", "'RSA-OAEP'");
    shouldBe("jwkPublicKey.ext", "true");
    shouldBe("jwkPublicKey.key_ops", "['encrypt', 'wrapKey']");
    shouldBe("jwkPublicKey.use", "undefined");
    shouldBe("jwkPublicKey.kty", "'RSA'");
    shouldBe("bytesToHexString(Base64URL.parse(jwkPublicKey.e))", "'010001'");

    debug("\nImporting it back...");
    return crypto.subtle.importKey("jwk", jwkPublicKey, { name: "RSA-OAEP", hash: {name: "sha-1"} }, extractable, ["encrypt", "wrapKey"]);
}).then(function(result) {
    exportedPublicKey = result;
    shouldBe("exportedPublicKey.type", "'public'");
    shouldBe("exportedPublicKey.algorithm.name", "'RSA-OAEP'");
    shouldBe("exportedPublicKey.algorithm.modulusLength", "512");
    shouldBe("bytesToHexString(exportedPublicKey.algorithm.publicExponent)", "'010001'");
    shouldBe("exportedPublicKey.algorithm.hash.name", "'SHA-1'");
    shouldBe("exportedPublicKey.extractable", "true");
    shouldBe("exportedPublicKey.usages", "['encrypt','wrapKey']");

}).then(finishJSTest, failAndFinishJSTest);

</script>

</body>
</html>
